package com.lft.freshordering.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.lft.freshordering.entity.Audit;
import com.lft.freshordering.entity.User;
import com.lft.freshordering.mapper.AuditMapper;
import com.lft.freshordering.mapper.UserMapper;
import com.lft.freshordering.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuditMapper auditMapper;
//    private String appid="wxf1f22625bfda0a13";
//    private String secret="d86ae4c4684373a5eedb8460c0b0cdb3";

//ght
    private String appid="wx6bb06fe951e4a2b8";
    private String secret="1854b03c58f37453d4ac64a164492989";

    @Override
    public String getOpenId(String code) {
        String authUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
        String result = HttpUtil.get(authUrl);
        String openid = JSONUtil.parseObj(result).getStr("openid");
        return openid;
    }

    @Override
    public int insertUser(String openid) {
       userMapper.insertUser(openid);
        return 0;
    }

    @Override
    public User selectOpenid(String openid) {
        return userMapper.selectUserByOpenid(openid);
    }

    @Override
    public Audit selectAuditStatus(Integer userId) {
        return auditMapper.selectAuditStatus(userId);
    }
}
